/**************************************************************************
 * Copyright (C) 2017-2017  Unisound
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **************************************************************************
 *
 * Description : uni_lasr.c
 * Author      : wufangfang@unisound.com
 * Date        : 2019.09.18
 *
 **************************************************************************/

#include "uni_lasr.h"
#include "uni_iot.h"
#include "uni_log.h"
#include "uni_hal_mailbox.h"

#define TAG     "lasr"

#pragma pack(push)
#pragma pack(4)           // 4 byte alignment
typedef struct{
  uni_u32   start_mode;   // 0 means wakeup mode, 1 means cmd mode
  uni_u32 timeout;   //ms
  uni_u32 session_id;
}kws_start_msg_t;

typedef struct{
  uni_u32   session_id;
}kws_timeout_msg_t;

typedef struct{
  uni_u32   start_res;
  uni_u32   stop_res;
  uni_u32   result_base;
  uni_u32   result_len;
  uni_u32   session_id;
}kws_result_msg_t;
#pragma pack(pop)

typedef enum {
  LASR_STATE_INIT = 0,
  LASR_STATE_IDLE,
  LASR_STATE_WORKING,
  LASR_STATE_MAX
}lasr_state_e;

typedef struct {
  lasr_state_e      state;
  _cb_lasr_result   cb_result;
  _cb_lasr_timeout  cb_timeout;
}uni_lasr_context_t;

static uni_lasr_context_t g_lasr_context = {LASR_STATE_INIT, NULL, NULL};

static int _recv_lasr_result(char *args, mailbox_message *message) {
  kws_result_msg_t *result_msg = (kws_result_msg_t *)message->data;
  if (NULL != g_lasr_context.cb_result && NULL != result_msg) {
#if !HIFI_MOCK_TEST
    invalidate_dcache_range(result_msg->result_base,
                            result_msg->result_base + result_msg->result_len);
#endif
    g_lasr_context.cb_result(result_msg->session_id,
                             (char *)result_msg->result_base,
                             result_msg->start_res, result_msg->stop_res);
  }
  return 0;
}

static int _recv_lasr_timeout(char *args, mailbox_message *message) {
  kws_timeout_msg_t *timeout_msg = (kws_timeout_msg_t *)message->data;
  if (NULL != g_lasr_context.cb_timeout && NULL != timeout_msg) {
    g_lasr_context.cb_timeout(timeout_msg->session_id);
  }
  return 0;
}

static int _mailbox_init(void) {
  sys_hal_mailbox_register_callback(MAILBOX_KWS_RESULT,
                                    _recv_lasr_result, NULL);
  sys_hal_mailbox_register_callback(MAILBOX_KWS_TIMEOUT,
                                    _recv_lasr_timeout, NULL);
  return 0;
}

Result uni_lasr_init(_cb_lasr_result cb_result,
                         _cb_lasr_timeout cb_timeout) {
  if (LASR_STATE_INIT != g_lasr_context.state) {
    LOGR(TAG, "cannot init again !");
    return E_FAILED;
  }
  g_lasr_context.cb_result = cb_result;
  g_lasr_context.cb_timeout = cb_timeout;
  if (0 != _mailbox_init()) {
    LOGR(TAG, "_mailbox_init failed !");
    return E_FAILED;
  }
  g_lasr_context.state = LASR_STATE_IDLE;
  return E_OK;
}

void uni_lasr_final(void) {
  if (LASR_STATE_INIT != g_lasr_context.state) {
    uni_lasr_stop();
    g_lasr_context.state = LASR_STATE_INIT;
  }
}

Result uni_lasr_start(uni_u32 session_id, engine_lasr_mode mode, uni_u32 timeout) {
  mailbox_message msg;
  kws_start_msg_t *start = (kws_start_msg_t *)msg.data;
  if (LASR_STATE_INIT == g_lasr_context.state) {
    LOGR(TAG, "please init first !");
    return E_FAILED;
  }
  if (LASR_STATE_WORKING == g_lasr_context.state) {
    LOGR(TAG, "it's working now, cannot start again !");
    return E_FAILED;
  }
  msg.taskId = MAILBOX_KWS_START;
  msg.size = sizeof(kws_start_msg_t) / sizeof(uni_u32);
  start->start_mode = mode;
  start->timeout = timeout;
  start->session_id = session_id;
  LOGT(TAG, "sned MAILBOX_KWS_START !");
  sys_hal_mailbox_send(&msg);
  LOGT(TAG, "sned MAILBOX_KWS_START done !");
  g_lasr_context.state = LASR_STATE_WORKING;
  return E_OK;
}

void uni_lasr_stop(void) {
  mailbox_message msg;
  if (LASR_STATE_WORKING == g_lasr_context.state) {
    msg.taskId = MAILBOX_KWS_STOP;
    msg.size = 0;
    sys_hal_mailbox_send(&msg);
    g_lasr_context.state = LASR_STATE_IDLE;
  }
}

